package com.wgchao.authmanager.application.vo;

import com.wgchao.authmanager.infrastructure.exception.ObjectNotMatchingException;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: AuthoInfoVo
 * @author: wgchao
 * @createTime: 2020/5/9 7:28 PM
 * @Description: 授权信息
 */
@Data
@NoArgsConstructor
@ToString
public class AuthoInfoVo implements Serializable {
    private String projectIdentify;
    private String menuIdentify;
    private String menuName;
    private boolean authorize;
    List<AuthoHandleVo> handles;

    public void combine(AuthoInfoVo authoInfoVo){
        if(authoInfoVo.isAuthorize()){
            this.authorize = true;
        }
        if(authoInfoVo.handles!=null){
            this.handles = this.handles==null?new ArrayList<>():this.handles;
            this.handles.addAll(authoInfoVo.handles);
        }
        HashSet<AuthoHandleVo> handleVoHashSet = new HashSet<>();
        handleVoHashSet.addAll(this.handles);
        this.handles = handleVoHashSet.stream().collect(Collectors.toList());
    }

    @Override
    public int hashCode() {
        int result = projectIdentify != null ? projectIdentify.hashCode() : 0;
        result = 31 * result + (menuIdentify != null ? menuIdentify.hashCode() : 0);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof AuthoInfoVo)){
            throw new ObjectNotMatchingException("授权信息类型对比错误");
        }
        AuthoInfoVo AuthoInfoVo = (AuthoInfoVo) obj;
        if(this.projectIdentify.equals(AuthoInfoVo.projectIdentify) &&
                this.menuIdentify.equals(AuthoInfoVo.menuIdentify)){
            return true;
        }
        return false;
    }
}
